using System;

namespace ClearSpringNet.Util
{
    public class Pair<T1, T2>
    {
        public T1 left;
        public T2 right;

        public Pair(T1 left, T2 right)
        {
            this.left = left;
            this.right = right;
        }
        
        public int hashCode()
        {
            int hashCode = 31 + (left == null ? 0 : left.GetHashCode());
            return 31 * hashCode + (right == null ? 0 : right.GetHashCode());
        }

        public bool equals(Object o)
        {
            if (!(o.GetType() is Pair<T1, T2>))
                return false;

            var that = (Pair<T1, T2>)o;
            // handles nulls properly
            return equal(left, that.left) && equal(right, that.right);
        }
        
        private bool equal(Object a, Object b)
        {
            return a == b || (a != null && a.Equals(b));
        }

        public String toString()
        {
            return "(" + left + "," + right + ")";
        }

        public static Pair<X, Y> create<X, Y>(X x, Y y)
        {
            return new Pair<X, Y>(x, y);
        }
    }
}